<?php
class Model_Tree extends Zend_Db_Table_Abstract {
	protected $DataAp;
	public function __construct() {
		$this->DataAp = Zend_Registry::get ( "db" );
	}
	
	public function getchidelpage($id,$rows,$offset) {
		$result = array();
		if($id==1){
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM catalog where  parentId=1 and name != ''" );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];       
		$query = $this->DataAp->query ( "SELECT id ,name,isCat,parentId,content  FROM catalog where parentId=1 and name != '' LIMIT $offset, $rows" );
		$data = $query->fetchAll ();
		 $items = array();
		 foreach($data as &$row)
		 {
		  if($row['isCat'] > 0){
			$row['state'] =  'closed';}
          else{
		 	$row['state'] =  'open';
		  }
		  array_push($items, $row);
		 }
		  $result["rows"] = $items;   
		}
		else{
			$query = $this->DataAp->query ( "SELECT id ,name,price,isCat,type, parentId,content,status,quantity  FROM catalog where parentId=$id and name != ''  LIMIT $offset, $rows" );
		    $data = $query->fetchAll ();
			   foreach($data as &$row)
			   {
				 if($row['isCat'] > 0){
				 $row['state'] = 'closed';}
			     else{
				 $row['state'] = 'open';
			     }
		  array_push($result, $row);
		    }
		}
		return $result;
	}

	public function getbcsp($id) {       
		$query = $this->DataAp->query ( "SELECT cl.id ,cl.name as name,cl.quantity as scl , SUM(db.quantity) AS sdb,SUM(nk.quantity) AS snk 
		FROM catalog cl  
		 INNER JOIN storeproduct nk ON nk.productId = cl.id
		 INNER JOIN orderproduct db ON db.productId = cl.id
		WHERE cl.id = $id and db.productId = $id and nk.productId = $id" );
		$data = $query->fetchAll ();
		return $data;
	}
	
	
	public function getItems($id) {
		$result = array();        
		$query = $this->DataAp->query ( "SELECT id ,name,price,isCat,type,quantity, parentId FROM catalog where parentId=$id and name != '' " );
		 $data = $query->fetchAll ();
		 foreach($data as &$row)
		 {
		  if($row['isCat'] > 0){
			$row['state'] =  'closed';}
          else
		 	$row['state'] =  'open';
		 }
		return $data;
	}
	public function getItemscombo($id) {
		$result = array();        
		$query = $this->DataAp->query ( "SELECT id , name as text, parentId,isCat,price,type  FROM catalog where parentId=$id and isCat=1" );
		 $data = $query->fetchAll ();
		 foreach($data as &$row)
		 {
		  if($row['isCat'] > 0){
			$row['state'] =  'closed';}
          else
		 	$row['state'] =  'open';
		 }
		return $data;
	}
	
	
	
	
	public function dupliObj($id, $ten) {
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM catalog WHERE name = '$ten'" );
		if ($id != 0) {
			$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM catalog WHERE   id != $id AND name = '$ten'" );
		}
		$row = $query->fetchAll ();
		$total = $row [0] ['Total'];
		return $total;
	}
	
	
	public function add($data) {
		$Id = $this->DataAp->insert ( 'catalog', $data );
		return $Id;
	} 
	

	/**
	 * - Sửa dữ liệu vào bảng.
	 *
	 * @param Number $id        	
	 * @param Array $data        	
	 * @return Int
	 */
	public function update($id, $data) {
		$Id = $this->DataAp->update ( 'catalog', $data, 'id = ' . $id );
		return $Id;
	}
	public function isstatus($id, $data) {
		$Id = $this->DataAp->update ( 'catalog', $data, 'id = ' . $id );
		return $Id;
	}
	public function del($id) {
		$Id = $this->DataAp->delete ( 'catalog', 'id = ' . $id );
		return $Id;
	}
	
	
	public function searObj($ten,$kieu ,$sort, $order, $offset, $rows) {
		$result = array ();
		$where = " name LIKE '%$ten%' ";
		if ($kieu != '')
			$where = $where . "AND type = '$kieu' ";
		// Lay tong so ban ghi
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM catalog
				WHERE  $where " );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];
		// Thuc hien cau lenh truy xuat du lieu
		$query = $this->DataAp->query (" SELECT id ,name,price,isCat,type,parentId,content,status,quantity FROM catalog
						WHERE $where and isCat = 0  ORDER BY $sort $order LIMIT $offset, $rows" );
		
		$result ['rows'] = $query->fetchAll ();
		return $result;
	}
}
?>